package com.webull.information.center.carwler.job;

import java.net.InetAddress;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.webull.information.center.carwler.common.model.CollectRecord;
import com.webull.information.center.carwler.common.model.parm.CollectParm;
import com.webull.information.center.carwler.dao.CollectRecordDao;
import com.webull.information.center.carwler.task.News_MctrolInStok_Task;

/**
 * http://www.moneycontrol.com/
 * 
 * @author shimingjun
 * @date 2016年8月26日 上午10:37:10
 * @version 1.0
 * @since JDK 1.8
 */
@Component
public class News_Mcontrol_IndiaStok_Job implements SimpleJob {
	protected final Logger logger = LogManager.getLogger(getClass());
	private @Autowired News_MctrolInStok_Task news_MctrolInStok_Task;
	private @Autowired CollectRecordDao collectRecordDao;
	private final String collectJob = "News_Mcontrol_IndiaStok_Job";
	private final Integer loop = 30;// 循环20次退出,获取 20*100

	// 印度市场股票
	public final static List<Integer> regionIds = Arrays.asList(new Integer[] { 12 });

	@Override
	public void execute(ShardingContext context) {
		process_0(regionIds);
	}


	public void process_0(List<Integer> shardingRegionIds) {
		Collections.shuffle(shardingRegionIds);
		logger.info("News_Mcontrol_IndiaStok_Job div size{};shardExchanges:{};",
				ArrayUtils.toString(shardingRegionIds));
		for (Integer reginoId : shardingRegionIds) {
			try {
				List<CollectRecord> records = collectRecordDao.search(collectParm(reginoId));
				Integer lastId = getLastTickeId(records);
				logger.info("News_Mcontrol_IndiaStok_Job shardExchanges start collectJob{} div ,reginoId:{},lastId{}",
						collectJob, reginoId, lastId);
				Integer lastId_ = news_MctrolInStok_Task.fullMcontrolStockNews(reginoId, lastId, loop);
				CollectRecord record = backRecord(reginoId);
				record.setLastTickerId(lastId_);
				Integer collId = collectRecordDao.add(record);
				logger.info("News_Mcontrol_IndiaStok_Job shardExchanges end collectJob{} div ,reginoId:{},collId{}",
						collectJob, reginoId, collId);
			} catch (Exception e) {
				logger.error("News_Mcontrol_IndiaStok_Job shardExchanges collectJob div error:" + reginoId, e);
			}

		}
	}

	private Integer getLastTickeId(List<CollectRecord> records) {
		if (records == null || records.isEmpty()) {
			return -1;
		} else {
			return records.get(0).getLastTickerId();
		}
	}

	private CollectParm collectParm(Integer regionId) {
		CollectParm parm = new CollectParm();
		parm.setCollectJob(collectJob);
		parm.setExchangeCode("region_code");
		parm.setExchangeId(regionId);
		parm.setRecordType(CollectRecord.RecordTypeConstants.NEWS);
		return parm;
	}

	private CollectRecord backRecord(Integer regionId) {
		CollectRecord record = new CollectRecord();
		record.setCollectJob(collectJob);
		record.setExchangeCode("region_code");
		record.setExchangeId(regionId);
		record.setRecordType(CollectRecord.RecordTypeConstants.NEWS);
		try {
			record.setCustomer(InetAddress.getLocalHost().getHostAddress());
		} catch (Exception e) {
		}
		return record;
	}

}
